﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Windows.Forms;
using EO.Web;


public partial class _Default : System.Web.UI.Page
{    
    string conString = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["korisnik"] != null)
            Response.Redirect("Pocetna.aspx");
        kopcinja();
    }
    protected void kopcinja()
    {
        ((ImageButton)Master.FindControl("imbMakedonija")).ImageUrl = "mk-c.gif";
        ((ImageButton)Master.FindControl("imbPocetna")).ImageUrl = "pocetna-c.gif";
        ((ImageButton)Master.FindControl("imbSvet")).ImageUrl = "svet-c.gif";
        ((ImageButton)Master.FindControl("imbSport")).ImageUrl = "sport-c.gif";
        ((ImageButton)Master.FindControl("imbNauka")).ImageUrl = "nauka-c.gif";
        ((ImageButton)Master.FindControl("imbKultura")).ImageUrl = "kultura-c.gif";
        ((ImageButton)Master.FindControl("imbSouBiznis")).ImageUrl = "sou-biz-c.gif";
    }
    protected void proveriVnesiKorisnik()
    {
        SqlConnection konekcija = new SqlConnection(conString);           

        string sqlString = "SELECT [user] " +
                         "FROM korisnik "+
                         "WHERE [user] = @user ";
        SqlCommand komanda = new SqlCommand(sqlString, konekcija);
        komanda.Parameters.AddWithValue("@user", txtIme.Text);
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = komanda;        
        DataSet ds = new DataSet();
        try
        {
            konekcija.Open();
            adapter.Fill(ds, "Korisnik");            
        }
        catch (Exception err)
        {
            lblPoraka.Text = err.Message;
        }
        finally
        {
            konekcija.Close();
        }
        int k = ds.Tables["Korisnik"].Rows.Count;
        if (k == 0)
        {
            sqlString = "INSERT INTO korisnik VALUES(@user,@lozinka,@mail)";

            komanda.Parameters.AddWithValue("@mail", txtMail.Text);
            komanda.Parameters.AddWithValue("@lozinka", txtLoz.Text);
            komanda.CommandText = sqlString;
            try
            {
                konekcija.Open();
                komanda.ExecuteNonQuery();
            }
            catch (Exception err)
            {
                lblPoraka.Text = err.Message;
            }
            finally
            {              
                konekcija.Close();
            }            
            DialogResult result = MessageBox.Show("Вашата регистрација беше успешна!", "Инфо", MessageBoxButtons.OK, MessageBoxIcon.Information,MessageBoxDefaultButton.Button1,MessageBoxOptions.DefaultDesktopOnly);
            
            Response.Redirect("Pocetna.aspx");                    
        }
        else
        {
            lblPoraka.Text = "Веќе постои корисник со тоа корисничко име!";
        }       
    }
    protected void btnPodnesi_Click(object sender, EventArgs e)
    {
        proveriVnesiKorisnik();
    }
    protected void lnkPocetna_Click(object sender, EventArgs e)
    {
        Response.Redirect("Pocetna.aspx");
    }
    protected void lnkNastan_Click(object sender, EventArgs e)
    {
        Response.Redirect("Nastan.aspx");
    }
}